// 引入全局的app.js文件对象
const app = getApp();
// pages/index/articleContent/articleContent.js
var WxParse = require('../../../wxParse/wxParse');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    article:{},
    url:'',
    userId:'',
    point:'',
    id:''
  },
  toCollection(){
    let that = this
    this.setData({
      'article.collects':that.data.article.collects+1
    })
    // console.log(that.data.article.collects);
    wx.request({
      method:'POST',
      url: that.data.url+'article/update',
      data:{
        id:that.data.id,
        collects:that.data.article.collects
      },
      success(res){
        // console.log(that.data.article.id)
        // console.log(that.data.article.collects)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.globalData.userInfo.point += 5;
    this.setData({
      url:app.globalData.URL,
      userId:app.globalData.userInfo.id,
      point:app.globalData.userInfo.point,
      id:options.id
    })

    this.ponint()
    // console.log(options.id)
    // 1 接受请求参数
    let id = options.id;
    this.getArticle(id);
  },
  //阅读量增加
  read(id){
    let that = this;
    // 2 根据请求参数调用后端接口，获取动态数据
    wx.request({
      method:'POST',
      url: that.data.url+'article/update',
      data:{
        id:id,
        readquantity:that.data.article.readquantity+1
      },
      success(res){
        // console.log(that.data.article)
        // console.log(that.data.article.readquantity)
      }
    })
  },
  //获得文章对象
  getArticle(id){
    let that = this;
    // 2 根据请求参数调用后端接口，获取动态数据
    wx.request({
      method:'GET',
      url: that.data.url+'article/find',
      data:{
        id:id
      },
      success(res){
        // 3 把数据赋值给data中的对象
        let result = res.data.data.list[0];
        WxParse.wxParse('content', 'html', result.details, that, 5);
        that.setData({
          article:result
        })
        // console.log(that.data.article)
        that.read(id);
      }
    })
  },
  //增加积分
  ponint(){
    let that = this;
    // 2 根据请求参数调用后端接口，获取动态数据
    wx.request({
      method:'PUT',
      url: that.data.url+'user',
      data:{
        id:that.data.userId,
        point:that.data.point
      },
      success(res){
        // console.log(that.data.point)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})